"""
    调用微博登录
"""
import requests
from flask import Flask, redirect, request
app = Flask(__name__)


# 获取ticket
def get_ticket():
    # 请求地址
    URL = 'https://api.weibo.com/oauth2/authorize?client_id=1501347784&response_type=code&redirect_uri=http://www.baidu.com'
    return URL


# 用ticket换取token
def get_token(code):
    URL = 'https://api.weibo.com/oauth2/access_token?client_id=1501347784&client_secret=fe61f5c2d7887f37b231b1ca7e2c4abf&grant_type=authorization_code&redirect_uri=http://www.baidu.com&code=' + code
    RES = requests.post(URL)
    return RES.json()


@app.route('/weibo')
def weibo():
    TICKET = get_ticket()
    return redirect(TICKET)


@app.route('/')
def index():
    CODE = request.args.get('code', None) # 获取请求头的参数
    TOKEN = get_token(CODE)
    return str(TOKEN)


if __name__ == '__main__':
    app.run(debug=True)
